#include <iostream>
#include <fstream>

double** ADCMapAdd(double** map,int& count,double x,double y,double size,double color)
{
	double** newMap = new double*[count+1];

	for(int i = 0;i < count;i++) newMap[i] = map[i];
	delete map;

	newMap[count] = new double[4];

	newMap[count][0] = x;
	newMap[count][1] = y;
	newMap[count][2] = size;
	newMap[count][3] = color;

	count++;

	return newMap;
}

double** ReadMap(char* filePath,int& totalNum)
{
	int ADCMapCount = 0;
	double** ADCMap = new double*[ADCMapCount];

	std::ifstream file;
	file.open(filePath);

	char* number = new char[1024];
	for(int j = 0;j < 1024;j++) number[j] = ' ';
	int numberCount = 0;

	bool ADCFlag = true;
	int ADCNumber = 0;

	int color = 0;
	double size = 2.5;

	while(file.good())
	{
		char c = file.get();
		if(c == '\t')
		{
			if(ADCFlag)
			{
				ADCNumber = atoi(number);
				color = 1+ADCNumber%6;
				size  = 2.2 - (ADCNumber%5)*.425;
				ADCFlag = false;
			}
			else
			{
				int num = atoi(number);
				int i;
				int j;
				if(num < 3000)
				{
					j = num%48;
					i = (num-j)/48;
					ADCMap = ADCMapAdd(ADCMap,ADCMapCount,(i+.5-24)*2.5,(j+.5-24)*2.5,size,color);
				}
				else
				{
					j = (num-3000)%36;
					i = (num-3000-j)/36;
					ADCMap = ADCMapAdd(ADCMap,ADCMapCount,(i+.5-18)*5,(j+.5-18)*5,size,color);
				}
			}
			numberCount = 0;
			for(int j = 0;j < 1024;j++) number[j] = ' ';
			continue;
		}
		if(c == '\n')
		{
			if(!ADCFlag)
			{
				int num = atoi(number);
				int i;
				int j;
				if(num < 3000)
				{
					j = num%48;
					i = (num-j)/48;
					ADCMap = ADCMapAdd(ADCMap,ADCMapCount,(i+.5-24)*2.5,(j+.5-24)*2.5,size,color);
				}
				else
				{
					j = (num-3000)%36;
					i = (num-3000-j)/36;
					ADCMap = ADCMapAdd(ADCMap,ADCMapCount,(i+.5-18)*5,(j+.5-18)*5,size,color);
				}
			}
			ADCFlag = true;
			numberCount = 0;
			for(int j = 0;j < 1024;j++) number[j] = ' ';
			continue;
		}

		number[numberCount] = c;
		numberCount++;
	}

	delete number;

	totalNum = ADCMapCount;
	file.close();
	return ADCMap;
}
